<?php
	// On prolonge la session
	session_start();
	$id_anim='';
	$code='';
	
	require_once dirname(__FILE__) . "/../../includes/config.php";
	require_once PHP_ROOTPATH.'/classes/database.php';


	$idconnect=Database::getInstance();
	//vérification des droits de l'utilisateur
	$querytest = pg_query($idconnect,"SELECT id_user, id_droit
  									  FROM droit_utilisateur
 									  where (id_droit = 4 or id_droit = 1) and  id_user =".$_SESSION['membre_id']);
	$lecture_biobanque = pg_num_rows($querytest);
	if($lecture_biobanque == 0){
		header('Location: ../recherche.php');
		exit();
	}
	else{
		// On teste si la variable de session existe et contient une valeur
		if(empty($_SESSION['id_anim'])){
			// Si inexistante ou nulle, on redirige vers la page de saisie du code
			//header('Location: ../recherche.php');
			//exit();
			echo 'test';
		}
		else{
			$id_anim=$_SESSION['id_anim'];
			$code=$_SESSION['code'];
		}
	}
?>

<!DOCTYPE html>
<html>
	<head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="../../style.css" />
		<!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <!--[if lte IE 7]>
        <link rel="stylesheet" href="../../../../style_ie.css" />
        <![endif]-->
        <title>Base de données microcèbes</title>
        
        <script type="text/javascript" src="../../js/oXHR.js"></script>
        <script>
			function request(oSelect){
				//récupération des informations sur les tissus de l'animal
				var value = oSelect.options[oSelect.selectedIndex].text;
				var elt = document.getElementById("detail");
				var id = <?php echo $id_anim; ?>;
				
				if(value!="none"){
					elt.style.display="inline";
					var var1 = encodeURIComponent(value);
					var xhr = getXMLHttpRequest();

					xhr.onreadystatechange = function() {
						if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
							readData(xhr.responseXML);
						} 
					};
					xhr.open("POST", "get_biobanque.php", true);
					xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					xhr.send("type_tissu=" + var1+"&id=" + id);
				}
				else{
					elt.style.display="none";
				}
			}
			function readData(oData){
				//affichage des champs récupérés
				var nodes   = oData.getElementsByTagName("item");
				var oSelect = document.getElementById("detail");
				var oOption, oText, oInner, oInner2, h1;
				var section1 = document.createElement("section");
				var section2 = document.createElement("section");
				var para1,para2;
				oSelect.innerHTML = "";
				
				var cmd=0;
				for (var i=0, c=nodes.length; i<c; i++) {
					oOption = document.createElement("label");
					oText = document.createElement("strong");
					oSpace = document.createElement("br");
					if(nodes[i].getAttribute("id")=="Commande" || nodes[i].getAttribute("id")=="Quantité restante"){
						h1 = document.createElement("h1");
						var textH1 = document.createTextNode (nodes[i].getAttribute("id"));
						h1.appendChild(textH1);
						section1.appendChild(h1);
						para1 = document.createElement("p");
						section1.appendChild(para1);
						cmd=1;
					}
					else if(nodes[i].getAttribute("id")=="Informations"){
						h1 = document.createElement("h1");
						var textH1 = document.createTextNode ("Informations sur le tissu:");
						h1.appendChild(textH1);
						section2.appendChild(h1);
						para2 = document.createElement("p");
						section2.appendChild(para2);
						cmd=2;
					}
					else{
						oInner2 = document.createTextNode(nodes[i].getAttribute("id")+": ");
						oInner  = document.createTextNode(nodes[i].getAttribute("name"));
						oText.appendChild(oInner2);
						oOption.appendChild(oText);
						oOption.appendChild(oInner);
					
						if(cmd==1){
							para1.appendChild(oOption);
							para1.appendChild(oSpace);
							oSelect.appendChild(section1);
						}
						else if(cmd==2){
							para2.appendChild(oOption);
							para2.appendChild(oSpace);
							oSelect.appendChild(section2);
						}
					}
				}
			}
        </script>
    </head>
    
    <body>
		<?php include((dirname(dirname(dirname(__FILE__))))."/menu.php"); ?>

		<section id="main">
			<header>
				<h1>Recherche - Biobanque/Dissection animal <?php echo $code;?></h1>
			</header>
			<?php include((dirname(dirname(__FILE__)))."/menu_recherche.php"); ?>
			<section id="animal">
				<div id="menu3">
					<ul id="onglets">
						<li class="active"><a href="biobanque.php">Biobanque</a></li>
						<li><a href="dissection.php">Dissection</a></li>
					</ul>
				</div>
				
				<section id="contenu">
					<h1>Sélectionnez un type:</h1>
					<p>
						<select id="Listetissus" onchange="request(this)">
						<option value="none">Sélection</option>
						<?php 
							$idconnect=Database::getInstance();
							
							//récupération de la liste des tissus			 
							$query = pg_query($idconnect,"SELECT DISTINCT(type_tissu) FROM biobanque WHERE id_animal='".$id_anim."'");
							$int = pg_num_rows($query);
							if($int==0){echo("<p><label>Pas d'informations disponibles</label></p>");}
							else{
								$i=0;
								while ($back = pg_fetch_assoc($query)) {
									echo "<option value='".$i."'>".$back["type_tissu"]."</option>";
									$i++;
								}
							} 
						?>
						</select><br/>
					</p>
					<section id="detail"></section>
				</section>
			</section>
			
		</section>
	</body>
</html>
